function exist=align_image(imagepath,AlignedPath)

imgSize = [ 250 250 ];
baseCoords = [ 95 155 125; ...
    127 127 185];
im = imread(imagepath);
[exist,points]=detect_image(imagepath);
if exist==0
    exit;
end
% get pts of left eye center
left_eye_x = (points(20,1)+points(21,1)+points(22,1)+points(23,1)+points(24,1)+points(25,1))/6;
left_eye_y = (points(20,2)+points(21,2)+points(22,2)+points(23,2)+points(24,2)+points(25,2))/6;

% get pts of right eye center
right_eye_x = (points(26,1)+points(27,1)+points(28,1)+points(29,1)+points(30,1)+points(31,1))/6;
right_eye_y = (points(26,2)+points(27,2)+points(28,2)+points(29,2)+points(30,2)+points(31,2))/6;

% get pts of mouth center
mouth_x = (points(32,1)+points(35,1)+points(38,1)+points(41,1))/4;
mouth_y = (points(32,2)+points(35,2)+points(38,2)+points(41,2))/4;

points_1(1,1) = left_eye_x;
points_1(1,2) = left_eye_y;
points_1(2,1) = right_eye_x;
points_1(2,2) = right_eye_y;
points_1(3,1) = mouth_x;
points_1(3,2) = mouth_y;
% show points
%         imshow(im); hold on;
%         for j = 1:size(points_1,1)
%             plot(points_1(j,1),points_1(j,2),'g*','markersize',2);
%         end
%         hold off

%% three points transformation
pts = points_1';
S = ThreePointAffine( baseCoords, pts)+ randn(2,3)*0.0;
T_in = double([S ; 0 0 1]) ;
Tfm = fliptform(maketform('affine',T_in'));

% alignment
I   = (imtransform(im, Tfm,'bicubic','XData',[1 imgSize(2)],'YData',[1 imgSize(1)],'Size',imgSize));
imwrite(I,[AlignedPath ]);
end